;;; GNU Guix --- Functional package management for GNU
;;;
;;; Copyright © 2021 jgart <jgart@dismail.de>
;;;
;;; This file is not part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guixrus packages vis)
  #:use-module (guix build utils)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system copy)
  #:use-module (guix gexp)
  #:use-module (guix git-download)
  #:use-module (guix packages)
  #:use-module (guix utils)
  #:use-module (gnu packages)
  #:use-module (gnu packages lua)
  #:use-module (gnu packages text-editors)
  #:export (vis-static))

(define vis-lspc
  (let ((commit "47b7b1e47bb10183f5b4ee84d90b4e3eefabe75c")
        (revision 1))
    (origin
      (method git-fetch)
      (uri
       (git-reference
        (url "https://gitlab.com/muhq/vis-lspc")
        (commit commit)))
      (file-name (git-file-name "vis-lspc" commit))
      (sha256
       (base32 "0kc1gsrybvim3pvvsq6mjw14jasp5idxd9ng2p8299pwxhm414k8")))))

(define-public vis-lsp
  (let ((commit "6635d0d2430aa2c74614e28f58c10318e798a839")
        (revision "0"))
    (package
      (inherit vis)
      (name "vis-lsp")
      (version (git-version "20210512" revision commit))
      (source
       (origin
         (method git-fetch)
         (uri
          (git-reference
           (url "https://github.com/fischerling/vis")
           (commit commit)))
         (sha256
          (base32 "1nccij2kyd8nllxmvdfvyprgwvv6gy28g36zy3x27xjr4j7s8894"))
         (file-name (git-file-name name version))))
      (build-system gnu-build-system)
      (inputs
       `(,@(package-inputs vis)
         ("vis-lspc" ,vis-lspc)))
      (arguments
       `(#:tests? #f                    ; TODO
         ,@(package-arguments vis)
         #:phases
         (modify-phases %standard-phases
           (add-after 'install 'wrap-binary
             (lambda* (#:key inputs outputs #:allow-other-keys)
               (let* ((out (assoc-ref outputs "out"))
                      (lpeg (assoc-ref inputs "lua-lpeg"))
                      (lua-version ,(version-major+minor (package-version lua)))
                      (LUA_PATH (string-append lpeg "/share/lua/"
                                               lua-version "/?.lua"))
                      (LUA_CPATH (string-append lpeg "/lib/lua/"
                                                lua-version "/?.so")))
                 (wrap-program (string-append out "/bin/vis")
                   `("LUA_PATH" ":" prefix (,LUA_PATH))
                   `("LUA_CPATH" ":" prefix (,LUA_CPATH))))))
           (add-after 'install 'bundle-vis-lspc
             (lambda* (#:key inputs outputs #:allow-other-keys)
               (let ((vis-lspc (string-append (assoc-ref outputs "out") 
                                              "/share/vis/plugins/vis-lspc")))
                 (mkdir-p vis-lspc)
                 (copy-recursively (assoc-ref inputs "vis-lspc") 
                                   vis-lspc)))))))
      (home-page "https://github.com/martanne/vis/pull/675")
      (synopsis "Vis with Language Server Protocol support")
      (description "@command{vis-lsp} provides vis with the communication API
patches applied.  Additionally, @code{vis-lspc} is bundled for convenience."))))

(define-public static-vis
  (let ((vis (static-package vis)))
    (package (inherit vis)
      (name "vis-static"))))

(define-public vis-guava
  (let ((commit "4293e57e43b68c3611d0766be1f96f38629ed775")
          (revision "0"))
    (package
      (name "vis-guava")
      (version "0.1")
      (source
       (origin
         (method git-fetch)
         (uri (git-reference
               (url "https://git.sr.ht/~whereiseveryone/vis-guava")
               (commit commit)))
         (file-name (string-append name "-" commit))
         (sha256
          (base32
           "1gphmccyzk76payqd9gg91fz759hy5fwag7nk18f2wdba3c9ls3k"))))
      (build-system copy-build-system)
      (arguments
       '(#:install-plan
         '(("guava.lua" "share/vis/themes/"))))
      (synopsis "Guava-flavoured theme for the vis text editor")
      (description
"@code{vis-guava} is a guava-flavoured theme for the vis text editor.")
      (home-page "https://git.sr.ht/~whereiseveryone/vis-guava")
      (license license:gpl3+))))

